package io.zso.commons.security.context;

import io.zso.commons.security.user.UserDetail;
import io.zso.commons.tools.enums.SuperAdminEnum;
import io.zso.commons.tools.utils.HttpContextUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 租户
 *
 * @author hp@zso.io
 */
public class TenantContext {

    public static String getTenantCode(UserDetail user){
        if(user.getTenantCode() == null){
            return null;
        }

        //超级管理员，才可以切换租户
        if(user.getSuperAdmin() == SuperAdminEnum.YES.value()){
            String tenantCode = HttpContextUtils.getTenantCode();
            if(StringUtils.isNotBlank(tenantCode)){
                return tenantCode;
            }
        }
        return user.getTenantCode();
    }
}
